/**
 * CaddieTransferHandler.java 21 juil. 07
 *
 * Copyright (c) 2007 windu.2b <jcaddie@windu2b.org>. All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.windu2b.jcaddie.swing;

import java.awt.*;
import java.awt.datatransfer.*;
import java.io.*;
import java.util.*;
import java.util.List;

import javax.swing.*;

import org.windu2b.jcaddie.model.*;

/**
 * @author windu.2b
 * 
 */
public class CaddieTransferHandler extends LocatedTransferHandler
{
	/**
	 * 
	 */
	private static final long	serialVersionUID	= 1149439215288082921L;


	private Caddie	          caddie;


	private CaddieController	caddieController;


	private List<Object>	  copiedItems;




	public CaddieTransferHandler( Caddie caddie,
	        CaddieController caddieController )
	{
		this.caddie = caddie;
		this.caddieController = caddieController;
	}




	@Override
	public int getSourceActions( JComponent source )
	{
		return COPY_OR_MOVE;
	}




	@Override
	protected Transferable createTransferable( JComponent source )
	{
		copiedItems = caddie.getSelectedItems();

		return new CaddieTransferableList( copiedItems );
	}




	@Override
	protected void exportDone( JComponent source, Transferable data, int action )
	{
		if ( action == MOVE ) caddieController.cut( copiedItems );

		copiedItems = null;
		caddieController.enablePasteAction();
	}




	@Override
	public boolean canImport( JComponent destination, DataFlavor[] flavors )
	{
		return Arrays.asList( flavors ).contains(
		        CaddieTransferableList.CADDIE_FLAVOR );
	}




	@SuppressWarnings( "unchecked" )
	@Override
	public boolean importData( JComponent destination, Transferable transferable )
	{
		if ( canImport( destination, transferable.getTransferDataFlavors() ) )
		{
			try
			{
				List<Object> items = ( List<Object> ) transferable
				        .getTransferData( CaddieTransferableList.CADDIE_FLAVOR );

				if ( isDrop() )
				{
					float x = 0;
					float y = 0;
					if ( destination instanceof CoursesComponent )
					{
						CoursesComponent caddieView = ( CoursesComponent ) destination;

						Point dropLocation = getDropLocation();
						SwingUtilities.convertPointFromScreen( dropLocation,
						        caddieView );

						x = caddieView.convertXPixelToModel( dropLocation.x );
						y = caddieView.convertYPixelToModel( dropLocation.y );
					}

					caddieController.drop( items, x, y );
				}
				else caddieController.paste( items );

				return true;
			}
			catch ( UnsupportedFlavorException ex )
			{
				throw new RuntimeException( "Can't import", ex );
			}
			catch ( IOException ex )
			{
				throw new RuntimeException( "Can't access to data", ex );
			}
		}
		else return false;
	}
}